package corp.edu.amadeus.core.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.apache.log4j.Logger;
import java.util.List;

public class BeanLocator {

	private static ApplicationContext ctx = new GenericApplicationContext();
	private static boolean configure = false;
	private static Logger log = Logger.getLogger(BeanLocator.class);

	public static void configure(List<String> configFiles,
		boolean fromClassPath) {
		String[] springConfigFiles = new String[configFiles.size()];
		for(int i=0;i<configFiles.size();i++) {
			springConfigFiles[i]=configFiles.get(i);
		}
		if(fromClassPath) {
		    log.info("++++++Configuring Spring context from ClassPath");
		    ctx = new ClassPathXmlApplicationContext(springConfigFiles);
		}else {
		    log.info("++++++Configuring Spring context from FileSystem");
		    ctx = new FileSystemXmlApplicationContext(springConfigFiles);
		}
		configure = true;
	}

	public static Object getBean(String idBean) {
		if(!configure) {
			log.error("No se ha configurado el contexto " +
					"de aplicacion de Spring.");
		}
		return ctx.getBean(idBean);
	}

	public static boolean existBean(String idBean) {
	    return ctx.containsBean(idBean);
	}
}